<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\common\AdminController;

/**
 * @Access(login = false)
 */
class Login extends AdminController
{
    public function index()
    {
        if (is_login()) {
            $this->redirect(url('admin/index/index'));
        }

        $this->assign('meta_title', '管理员登录');
        $this->display('login/index');
    }

    public function verify_login()
    {
        $username = input('username');
        $password = input('password');

        // 图片验证码校验
        //if (!$this->checkVerify(input('post.verify'))) {
        //   $this->error('验证码输入错误！');
        //}

        // 验证用户名密码是否正确
        $user_object = model('admin/user');
        $user_info = $user_object->check_login($username, $password);
        if (!$user_info) {
            $this->error($user_object->getError());
        }

        // 验证用户是否是管理员
        if (!$user_info['gid']) {
            $this->error('用户没有管理员权限。');
        }

        $user_object->auto_login($user_info);
        $this->success('登录成功！', url('admin/index/index'));
    }

//     /**
//      * 生成验证码。
//      */
//     public function verify($vid = 1)
//     {
//         $verify = new Verify();
//         $verify->length = 4;
//         $verify->useCurve = false;
//         $verify->entry($vid);
//     }

//     /**
//      * 检测验证码
//      * @param  integer $id 验证码ID
//      * @return boolean 检测结果
//      */
//     protected function checkVerify($code, $vid = 1)
//     {
//         $verify = new Verify();
//         return $verify->check($code, $vid);
//     }
}
